 $(function() {
    if(jplist == null){jplist=[];}
    else{
        var xxitem = {id:0, title:'谢谢参与', name:'谢谢参与'};
        jplist.push(xxitem);
    }
    $("#wheelcanvas").rotate({
                            duration: 1500,
                            angle: 0,
                            animateTo: -90,
                            easing: $.easing.easeOutSine
                            });
    var jsonData = {};
    var isStart = false;

    /*传入奖品id返回角度*/
    function backangle(prizeid){
        var prizenum = jplist.length;
        var avegangle = 360/prizenum;
        var resangel = avegangle/2;
        $.each(jplist, function(index, row){
            if(parseInt(row.id) == parseInt(prizeid)){
                resangel = (prizenum - index - 1) * avegangle + resangel;
            }
        });
        return resangel - 90;
    }

    function lottery() {
        var start = function() {
            if (isStart) {
                return false
            }
            isStart = true;
            $.Dialog.loading();
            $.ajax({
                type: 'POST',
                url: playurl,
                dataType: 'json',
                cache: false,
                data:{id:target_id},
                error: function() {
                    $.Dialog.fail('呀, 转盘罢工了, 请联系客服！');
                    isStart = false;
                },
                success: function(json) {
                    switch(json.statu){
                        case 0:
                            var a = backangle(json.content.id);
                            var p = json.content.title;
                            var n = json.content.count;
                            if (p != "") {
                                $.Dialog.close();
                                document.getElementById("playyy").play();
                                $("#wheelcanvas").rotate({
                                    duration: 10000,
                                    angle: -90,
                                    animateTo: 3600+a,
                                    easing: $.easing.easeOutSine,
                                    callback: function() {
                                        if(json.content.id>0){
                                            openSuccessDialog(json.content.title, json.content.name);
                                        }
                                        else{
                                            openErrorDialog(n);
                                        }
                                        $('#count').text(n);
                                        isStart = false;
                                    }
                                });
                            } else {
                                $.Dialog.fail("没有设置奖品名称哦");
                                isStart = false;
                            }
                            break;
                        case 1:
                            $.Dialog.fail(json.content);
                            isStart = false;
                            break;
                    }
                }
            });
        };
        $("#startbtn").bind("touchstart click", start);
    }
    var colors = ["#ff6501", "#f6f09f", "#FFFFFF"];
    var colors1 = ["#ffffff", "#ee673c"];
    var cwidth = 227;
    var startAngle = 0;
    var arc = Math.PI / jplist.length*2;
    var spinTimeout = null;
    var spinArcStart = 10;
    var spinTime = 0;
    var spinTimeTotal = 0;
    var ctx;
    function draw() {
        drawRouletteWheel()
    }
    function drawRouletteWheel() {
        var canvas = document.getElementById("wheelcanvas");
        if (canvas.getContext) {
            var outsideRadius = 97;
            var textRadius = 80;
            var insideRadius = 20;
            ctx = canvas.getContext("2d");
            //创建新的图片对象
            var img = new Image();
            //指定图片的URL
            img.src = zpimg;
            //浏览器加载图片完毕后再绘制图片
            img.onload = function(){
                ctx.clearRect(0, 0, 227, 227);
                ctx.drawImage(img, 0, 0);
                ctx.strokeStyle = "#e9e8e5";
                ctx.lineWidth = 1;
                ctx.font = 'bold 14px 黑体';
                for (var i = 0; i < jplist.length; i++) {
                    var angle = startAngle + i * arc;
                    if (i % 2 == 1) {
                        var gradient = ctx.createRadialGradient(cwidth / 2, cwidth / 2, 0, cwidth / 2, cwidth / 2, cwidth / 2);
                        gradient.addColorStop(0, '#fff');
                        gradient.addColorStop(0.7, '#f6f09f');
                        ctx.fillStyle = gradient
                    } else {
                        ctx.fillStyle = colors[(i % 2)];
                    }
                    ctx.beginPath();
                    //计算片数
                    ctx.arc(cwidth / 2, cwidth / 2, outsideRadius, angle, angle + arc, false);
                    ctx.arc(cwidth / 2, cwidth / 2, insideRadius, angle + arc, angle, true);
//                    ctx.shadowBlur = 5;
//                    ctx.shadowColor = "#666";
                    ctx.stroke();
                    ctx.fill();
                    ctx.save();
//                    ctx.shadowOffsetX = -1;
//                    ctx.shadowOffsetY = -1;
//                    ctx.shadowBlur = 0;
//                    ctx.shadowColor = "rgb(220,220,220)";
                    ctx.fillStyle = "black";
                    ctx.translate(cwidth / 2 + Math.cos(angle + arc / 2) * textRadius, cwidth / 2 + Math.sin(angle + arc / 2) * textRadius);
                    ctx.rotate(angle + arc / 2 + Math.PI / 2);
                    var text = jplist[i].title;
                    //绘制文字
                    drawText(text,ctx,colors1[(i % 2)]);
//                    ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
                    ctx.restore();
                }
            };
        }
    }

    function drawText(text,ctx,c){
        ctx.fillStyle = c;
        for(var i=0;i<text.length;i++){
            ctx.fillText(text[i], -ctx.measureText(text[i]).width / 2, i*15);
        }
    }

    draw();
    lottery();
});

 function openSuccessDialog(title, name){
     var successHtml = "<div class='common_dialog lqcg'>"
         +"<h6>你的运气太好了！</h6><p class='p_10'>你获得了" + title + ", 奖品是" + name + "。</p>"
         +"<div class='tb'><a class='btn m_15 flex_1' href='#' onClick='$.Dialog.close();location.reload();'>去门店领取</a></div>"
         +"</div>";
     $.Dialog.open(successHtml);
 }
 function openErrorDialog(count){
     var successHtml = "<div class='common_dialog lqcg'>"
         +"<h6>很抱歉！没有中奖，还需继续努力</h6><notempty name='data.max_num'><p class='p_10'>你还有" + count + "次机会。</p></notempty>"
         +"<div class='tb'><a class='btn m_15 flex_1' href='#' onClick='$.Dialog.close();'>确 定</a></div>"
         +"</div>";
     $.Dialog.open(successHtml);
 }

 wx.ready(function () {
     wx.showOptionMenu();
     var shareObjtofriends = {
         title: werisiteshare.title,
         desc: werisiteshare.desc,
         link: 'http://mp.weixin.qq.com/s?__biz=MzA3MTA3NDM4NA==&mid=211445515&idx=5&sn=a85772ef7131f38a166f687544b50031#rd',
         imgUrl: werisiteshare.imgUrl,
         trigger: function (res) {
         },
         success: function (res) {
             $.get(werisiteshare.successtoshare + "&sharetype=tofriends&target_id=" + target_id, function(data){
                 switch(data.statu){
                     case 0:
                         $.Dialog.fail(data.content);
                         break;
                     case 1:
                         $.Dialog.success(data.content);
                         break;
                 }
             })
         },
         cancel: function (res) {

         },
         fail: function (res) {
//            alert(JSON.stringify(res));
         }
     };
     //分享朋友圈
     wx.onMenuShareTimeline(shareObjtofriends);
 });